Page({
    data: {
        id1: 'back',
        id2: 'clear',
        id3: 'negative',
        id4: '+',
        id5: '9',
        id6: '8',
        id7: '7',
        id8: '-',
        id9: '6',
        id10: '5',
        id11: '4',
        id12: '×',
        id13: '3',
        id14: '2',
        id15: '1',
        id16: '÷',
        id17: '0',
        id18: '.',
        id19: 'history',
        id20: '=',

        screenData: '0',
        lastIsOperator: false,
        operators: ['+', '-', '×', '÷', '.', '='],
        history: [],

    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.setData({history: (wx.getStorageSync('history') || [])});
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    history: function () {
        wx.navigateTo({
            url: '../history/history'
        })
    },
    clickbtn: function (e) {
        function cal(num1, oper, num2) {
            num1 = parseFloat(num1);
            num2 = parseFloat(num2);
            switch (oper) {
                case '+':
                    return num1 + num2;
                case '-':
                    return num1 - num2;
                case '×':
                    return num1 * num2;
                case '÷':
                    return num1 / num2;
            }
        }

        var id = e.target.id;
        var oldData = this.data.screenData;
        if ((oldData == '0' && id != '.') || oldData == null) {
            oldData = '';
        }
        oldData = oldData.toString()//oldData为数字类型时,做substring操作会error

        console.log(id, oldData);

        if (id == this.data.id1) {//back
            if (oldData.length == 1 || oldData == '') {
                data = '0';
            } else {
                data = oldData.substring(0, oldData.length - 1);
                console.log(data);
                if (data.length == 1 && this.data.operators.indexOf(data) > -1) {
                    data = '0';
                }
            }
            this.setData({screenData: data});
            return;
        }

        if (id == this.data.id2) {//clear
            this.setData({screenData: '0', lastIsOperator: false, tempResult: '0'});
            return;
        }

        if (id == this.data.id3) {//正负号--暂不处理
            if (oldData == '' || this.data.lastIsOperator) return;

            data = -oldData;
            this.setData({screenData: data});
            return;
        }

        if (id == this.data.id4 || id == this.data.id8 || id == this.data.id12 || id == this.data.id16) {//operator
            if (oldData == '') return;

            if (this.data.lastIsOperator) {
                oldData = oldData.substring(0, oldData.length - 1);
            }

            var data = oldData + id;
            this.setData({screenData: data, lastIsOperator: true});
            return;
        }

        if (id == this.data.id20) {//make result
            if (oldData == '' || this.data.lastIsOperator) return;

            console.log(oldData);

            var opers = ['+', '-', '×', '÷'];
            var num1;
            var oper;
            var operIndex;
            var num2;
            var result;


            for (var i = 0; i < oldData.length; i++) {
                c = oldData.charAt(i);

                if (i != 0 && opers.indexOf(c) != -1) {
                    if (num1 == null) {
                        num1 = oldData.substring(0, i);
                        oper = c;
                        operIndex = i;
                    } else {
                        num2 = oldData.substring(operIndex + 1, i);
                        num1 = result = cal(num1, oper, num2)

                        oper = c;
                        operIndex = i;
                    }
                }

                if (i == oldData.length - 1) {
                    num2 = oldData.substring(operIndex + 1, oldData.length);
                    result = cal(num1, oper, num2)
                }

            }
            console.log(result);
            this.setData({screenData: result, lastIsOperator: false});

            //存储到历史记录
            this.data.history.push(oldData + '=' + result);
            wx.setStorageSync('history', this.data.history);
            console.log(this.data.history);

            num1 = oper = operIndex = num2 = result = null;
            return;
        }

        var data = oldData + id;
        this.setData({screenData: data, lastIsOperator: false});

    }
});